package com.practice.Serialize.Server;

import com.practice.Serialize.pojo.SubscribeReq;
import com.practice.Serialize.pojo.SubscribeResp;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        SubscribeReq req = (SubscribeReq) msg;
        if (req.getUserName() != null) {
            System.out.println("server accept req that  " + req.toString());
        }
        ctx.writeAndFlush(resp(req.getSubReqID(),req.getSendTime() ));

    }

    private SubscribeResp resp(int id, long sendTime) {
        SubscribeResp resp = new SubscribeResp();
        resp.setSubReqID(id);
        resp.setRespCode(0);
        resp.setReceiveTime(System.currentTimeMillis());
        resp.setUseTime(System.currentTimeMillis() - sendTime);
        resp.setDesc("server Received That order, we`ll product it as fast as we can");
        return resp;
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}
